package cn.itwen.star.graph.core.service.impl;

import cn.itwen.star.graph.core.service.OllamaService;
import cn.itwen.star.graph.ollama.client.api.OllamaApi;
import cn.itwen.star.graph.ollama.client.pojo.OllamaChatRequest;
import cn.itwen.star.graph.ollama.client.pojo.OllamaChatRespone;
import cn.itwen.star.graph.ollama.client.pojo.OllamaMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

@Service
public class OllamaServiceImpl implements OllamaService {

    @Autowired
    private OllamaApi ollamaApi;

    /**
     * 翻译
     * @param prompt
     * @return
     */
    @Override
    public String translate(String prompt) {
        try {
            //创建消息体
            OllamaMessage message = new OllamaMessage();
            message.setRole("user");
            message.setContent(String.format("帮我以下内容翻译为英文:\n%s", prompt));
            //创建请求体
            OllamaChatRequest request = new OllamaChatRequest();
            request.setModel("qwen2.5:0.5b");
            request.setMessages(List.of(message));
            request.setStream(false);
            //发送请求
            Call<OllamaChatRespone> ollamaChatResponeCall = ollamaApi.chat(request);
            Response<OllamaChatRespone> response = ollamaChatResponeCall.execute();
            if (response.isSuccessful()){
                OllamaChatRespone body = response.body();
                return body.getMessage().getContent();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prompt;
    }
}
